﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class WZ_BatchModifyShader : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    [MenuItem("Slg/批量修改shader:Particles_Additive为Mobile_Particles_Additive")]
    static void ModifyShaderRefFromPA2MPA()
    {
        Transform[] trans = Selection.GetTransforms(SelectionMode.Unfiltered);
        List<Material> lst = new List<Material>();

        for (int i = 0; i < trans.Length; i++)
        {
            Renderer[] rends = trans[i].GetComponentsInChildren<Renderer>();
            for (int j = 0; j < rends.Length; j++)
            {
                if (rends[j].sharedMaterials != null && rends[j].sharedMaterials.Length > 1)
                {
                    Debug.Log("material length larger than 1", rends[j].gameObject);
                }
                if (rends[j].sharedMaterial == null)
                {
                    Debug.Log("sharedMaterial is null", rends[j].gameObject);
                    continue;
                }
                if (lst.IndexOf(rends[j].sharedMaterial) == -1)
                {
                    if (rends[j].sharedMaterial.shader == null)
                    {
                        Debug.Log("shader is null", rends[j].gameObject);
                        continue;
                    }
                    lst.Add(rends[j].sharedMaterial);
                }
            }
        }

        string targetShaderName = "Mobile/Particles/Additive";
        for (int i = 0; i < lst.Count; i++)
        {

            if (lst[i].shader.name == "Particles/Additive")
            {
                Debug.LogFormat(lst[i], "修改了{0}的shader为{1}", lst[i].name, targetShaderName);
                lst[i].shader = Shader.Find(targetShaderName);
            }
            else
            {
                Debug.Log(lst[i].shader.name);

            }
        }
        EditorApplication.SaveAssets();
    }

}
